package cn.wolfcode.car.business.web.controller;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping("/business/appointment")
public class AppointmentController {

    private static final String prefix = "business/appointment/";

    @Resource
    private IAppointmentService appointmentService;

    @RequiresPermissions("business:appointment:view")
    @RequestMapping("/listPage")
    public String listPage() {
        return prefix + "list";
    }

    @RequiresPermissions("business:appointment:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        return appointmentService.query(qo);
    }

    @RequiresPermissions("business:appointment:add")
    @RequestMapping("/addPage")
    public String addPage() {
        return prefix + "add";
    }

    // 新增
    @RequiresPermissions("business:appointment:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Appointment appointment) {
        appointmentService.save(appointment);
        return AjaxResult.success();
    }

    @RequiresPermissions("business:appointment:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model) {
        model.addAttribute("appointment", appointmentService.queryById(id));
        return prefix + "edit";
    }

    // 修改
    @RequiresPermissions("business:appointment:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Appointment appointment) {
        appointmentService.update(appointment);
        return AjaxResult.success();
    }

    // 取消预约
    @RequiresPermissions("business:appointment:cancel")
    @RequestMapping("/cancel")
    @ResponseBody
    public AjaxResult cancelHandler(Long id) {
        Appointment appointment = appointmentService.queryById(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus()))
            throw new BusinessException("只有状态为预约中的预约单才能取消");
        appointmentService.changStatus(id, Appointment.STATUS_CANCEL);
        return AjaxResult.success();
    }

    // 删除
    @RequiresPermissions("business:appointment:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(@ModelAttribute("ids") Long id) {
        appointmentService.deleteById(id);
        return AjaxResult.success();
    }

    // 到店
    @RequiresPermissions("business:appointment:arrival")
    @RequestMapping("/arrival")
    @ResponseBody
    public AjaxResult arrivalHandler(Long id) {
        Appointment appointment = appointmentService.queryById(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus()))
            throw new BusinessException("只有状态为预约中的预约单才能到店");
        appointmentService.changStatus(id, Appointment.STATUS_ARRIVAL);
        return AjaxResult.success();
    }

    // 结算单
    @RequiresPermissions("business:appointment:generateStatement")
    @RequestMapping("/generateStatement")
    @ResponseBody
    public AjaxResult generateStatement(@ModelAttribute("appointmentId") Long id) {
        return AjaxResult.success(appointmentService.generateStatement(id));
    }
}

